#include "dialogs.h"
#include "form/ui_Loading.h"
#include "form/ui_Message.h"


//===================================== MyLoading ========================================//

MyLoading* MyLoading::m_instance = nullptr;
QMutex MyLoading::m_mutex;
MyLoading::MyLoading(QWidget* parent) : QDialog(parent), m_ui(new Ui::LoadingDialog) {
    m_ui->setupUi(this);
    //去掉对话框的对话框风格
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    //设置背景透明，内容不透明
    this->setWindowOpacity(1);
    this->setAttribute(Qt::WA_TranslucentBackground);
    QMovie* movie = new QMovie(QString::fromUtf8(":/images/loading1.gif"), QByteArray(), this->parent());
    m_ui->label->setMovie(movie);
    movie->start();
    this->open();
}


void MyLoading::startLoading(QWidget* parent, QString msg) {
    m_mutex.lock();
    if (m_instance == nullptr) {
        m_instance = new MyLoading(parent);
    }
    m_instance->setMessage(msg);
    m_instance->show();
    m_mutex.unlock();
}

void MyLoading::stopLoading() {
    m_mutex.lock();
    if (m_instance != nullptr) {
        m_instance->hide();
    }
    m_mutex.unlock();
}

void MyLoading::setMessage(QString msg) {
    m_ui->titleLabel->setText(msg);
}

//===================================== MyMessageBox ========================================//

int MyMessageBox::information(QWidget* parent, QString text, QString acceptBtn, QString rejectBtn) {
    QPixmap icon(QString::fromUtf8(":/images/info.svg"));
    MyMessageBox box(parent, icon, text, acceptBtn, rejectBtn);
    return box.exec();
}

int MyMessageBox::warning(QWidget* parent, QString text, QString acceptBtn, QString rejectBtn) {
    QPixmap icon(QString::fromUtf8(":/images/warning.svg"));
    MyMessageBox box(parent, icon, text, acceptBtn, rejectBtn);
    return box.exec();
}

int MyMessageBox::error(QWidget* parent, QString text, QString acceptBtn, QString rejectBtn) {
    QPixmap icon(QString::fromUtf8(":/images/error.svg"));
    MyMessageBox box(parent, icon, text, acceptBtn, rejectBtn);
    return box.exec();
}

int MyMessageBox::question(QWidget* parent, QString text, QString acceptBtn, QString rejectBtn) {
    QPixmap icon(QString::fromUtf8(":/images/question.svg"));
    MyMessageBox box(parent, icon, text, acceptBtn, rejectBtn);
    return box.exec();
}

MyMessageBox::MyMessageBox(QWidget* parent, QPixmap& icon, QString text, QString acceptBtn, QString rejectBtn) : QDialog(parent), m_ui(new Ui::MessageDialog) {
    m_ui->setupUi(this);
    if (parent != nullptr) {
        this->setWindowTitle(parent->windowTitle());
    }
    //this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    this->setWindowModality(Qt::NonModal);
    m_ui->logoLabel->setPixmap(icon);
    //set msg
    QString msg;
    if (text.size() >= 72) {
        msg = text.mid(0, 71);
        msg.append("...");
    } else {
        msg = text;
    }
    m_ui->msgLabel->setText(msg);
    //show buttons
    if (!acceptBtn.isEmpty()) {
        m_ui->acceptBtn->setText(acceptBtn);
    } else {
        m_ui->acceptBtn->hide();
    }
    if (!rejectBtn.isEmpty()) {
        m_ui->rejectBtn->setText(rejectBtn);
    } else {
        m_ui->rejectBtn->hide();
    }
}

//qint64 MyInputDialog::getIntVal() {
//    return qint64();
//}

